home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / s_to_z / statone / dynarray.txt next >
Text File  |  1996-09-15  |  6KB  |  225 lines

  1. This is code for a dynamic array of Single.
  2.  
  3. This code does **not** resize the array once you have sized it--there's a procedure to do it that, which  is now empty and a function InitializeArrayValues, that will initialize extra elements when you increase the size of the array (just pass it the pointer to the array and the boundaries of the elements you want to initialize). 
  4.  
  5. The code seems to work, although the testing is not yet extensive.  
  6.  
  7. The code is set up to be easy to convert to other an array of another type. To do so:
  8.  
  9.  (1) Change the MAX_SIZE constant to an appropriate maximum number for the TYPE you will be using; and 
  10.  
  11. (2) Change the "TElement = Single" line  to "TElement = YourType."
  12.  
  13. NOTE: you **must** use the range checking in code that reads or writes the Value property or ugly things could happen.  
  14.  
  15. What follows is First, code for the Array object and Second sample code for calling the object.
  16.  
  17. ****************************************************************
  18. THE TRZSingleArray OBJECT CODE:
  19.  
  20. unit Rzsing3;
  21.  
  22. interface
  23.  
  24. uses
  25.       SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  26.       Forms, Dialogs;
  27.  
  28. const
  29.     MAX_SIZE = 16000;
  30.  
  31. type
  32.     TElement     = Single;
  33.         TIndex         = WORD;
  34.     PElement     = ^TElement;
  35.         TTheArray     = Array[1..MAX_SIZE] of TElement;
  36.         PArray          = ^TTheArray;
  37.         TSingleArray     = class(TComponent)
  38.   private
  39.   { Private declarations }
  40.     FSize:         TIndex;
  41.     FArray:         TTheArray;
  42.              FArrayPtr:     PArray;
  43.         FArrayAssigned: Boolean;
  44.              PROCEDURE     SetArrayValue(idx: TIndex;
  45.                              CONST NewElement: TElement);
  46.              FUNCTION         GetArrayValue(idx: TIndex): TElement;
  47.              PROCEDURE     CreateArray(CONST Size: TIndex);
  48.              PROCEDURE     DestroyArray;
  49.              PROCEDURE     InitializeArrayElements(CONST LoInit, HiInit:TIndex);
  50.  
  51.   protected
  52.   public
  53.              property    Value[idx: TIndex]: TElement
  54.                     read GetArrayValue
  55.         write SetArrayValue; default;
  56.              property    AddrOfElement[idx: TIndex]: PElement
  57.                  read GetElementAddress;
  58.   published
  59.         property     Size: TIndex
  60.                      read  FSize
  61.                       write FSize;
  62.              FUNCTION    CheckRange(CONST N: TIndex):BOOLEAN;
  63.              FUNCTION     SetSize(Size: TIndex): BOOLEAN;
  64.              constructor Create(AOwner: TComponent); override;
  65.              destructor Destroy; override;
  66.     end;
  67.  
  68. procedure register;
  69.  
  70. implementation
  71. {===========================================}
  72. CONSTRUCTOR TSingleArray.Create(AOwner: TComponent);
  73.     BEGIN
  74.             inherited Create(AOwner);
  75.                  TRY      IF FSize > 0
  76.                             THEN     CreateArray(Size)
  77.                              ELSE    FArrayAssigned := FALSE;
  78.                          FSize := 0;
  79.                       FINALLY inherited Destroy;
  80.                          END;
  81.             END;
  82.  
  83. DESTRUCTOR TSingleArray.Destroy;
  84.     BEGIN
  85.         DestroyArray;
  86.                  inherited Destroy;
  87.         END;
  88.  
  89. PROCEDURE TSingleArray.InitializeArrayElements
  90.             (CONST LoInit, HiInit: TIndex);
  91.     VAR
  92.         idx: TIndex;
  93.     BEGIN
  94.         FOR     idx := LoInit TO HiInit
  95.                      DO FArrayPtr^[idx] := 0.0;
  96.             END;
  97.  
  98. PROCEDURE TSingleArray.CreateArray(CONST Size: TIndex);
  99.     BEGIN
  100.         GetMem(FArrayPtr, Size * SizeOf(TElement));
  101.                  FSize := Size;
  102.                  InitializeArrayElements(1, FSize);
  103.                  FArrayAssigned := TRUE
  104.             END;
  105.  
  106. PROCEDURE TSingleArray.DestroyArray;
  107.     BEGIN
  108.         FreeMem(FArrayPtr, FSize * SizeOf(TElement));
  109.                  FArrayAssigned := FALSE;
  110.             END;
  111.  
  112. FUNCTION TSingleArray.CheckRange(CONST N: TIndex): BOOLEAN;
  113.     BEGIN
  114.                 IF     (N > FSize) OR (N < 1)
  115.                      THEN Result := FALSE
  116.                       ELSE Result := TRUE;
  117.                 END;
  118.  
  119. PROCEDURE TSingleArray.SetArrayValue(idx: TIndex;
  120.     CONST NewElement: TElement);
  121.     BEGIN
  122.         FArray[idx] := NewElement;
  123.             END;
  124.  
  125. FUNCTION TSingleArray.GetArrayValue(idx: TIndex): TElement;
  126.         BEGIN
  127.             Result := FArray[idx];
  128.             END;
  129.  
  130. FUNCTION TSingleArray.SetSize(Size: TIndex): BOOLEAN;
  131.  
  132.     {CHECK THE RANGE
  133.               =================================================================}
  134.             IF     (Size > MAX_SIZE) OR (Size < 1)
  135.                  THEN BEGIN     Result := FALSE;
  136.                           Exit;
  137.                                      END;
  138.  
  139.              {CHECK TO SEE IF USER IS CHANGING SIZE
  140.               ==================================================================}
  141.         IF Size = FSize
  142.                  THEN Exit;
  143.  
  144.              {SET THE SIZE
  145.               ==================================================================}
  146.              IF     FArrayAssigned = FALSE
  147.                   THEN    CreateArray(Size)
  148.                   ELSE {REALLOCATE ARRAY ROUTINE HERE};
  149.              END;
  150.  
  151. PROCEDURE Register;
  152.     BEGIN
  153.         RegisterComponents('Samples', [TSingleArray]);
  154.          END;
  155.  
  156. end.
  157.  
  158.  
  159. *********************************************
  160. CODE FOR A FORM THAT CALLS TSingleArray
  161.  
  162. unit Arryfrm3;
  163.  
  164. interface
  165.  
  166. uses
  167.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  168.   Forms, Dialogs, StdCtrls, rzsing3;
  169.  
  170. type
  171.   TForm1 = class(TForm)
  172.     Edit1: TEdit;
  173.     Button1: TButton;
  174.     Button2: TButton;
  175.     Label1: TLabel;
  176.     Edit2: TEdit;
  177.     Label2: TLabel;
  178.     procedure Button1Click(Sender: TObject);
  179.     procedure Button2Click(Sender: TObject);
  180.   private
  181.     { Private declarations }
  182.   public
  183.     { Public declarations }
  184.  
  185.   end;
  186.  
  187. var
  188.   Form1: TForm1;
  189.   TestArray: TSingleArray;
  190.  
  191. implementation
  192.  
  193. TYPE
  194.     PSingle = ^Single;
  195.  
  196. {$R *.DFM}
  197.  
  198. PROCEDURE TForm1.Button1Click(Sender: TObject);
  199.     BEGIN
  200.         TestArray := TSingleArray.Create(Self);
  201.                  Edit1.Text := inttostr(TestArray.Size);
  202.         {Shows the size of the array at initialization. Should be 0}
  203.         END;
  204.                  IF NOT (TestArray.SetSize(27) = TRUE)
  205.                      THEN {RAISE EXCEPTION HERE};
  206.         IF TestArray.CheckRange(25) = TRUE
  207.             THEN TestArray[25] := 24.5;
  208.         IF TestArray.CheckRange(25) = TRUE
  209.             THEN     Edit2.Text := FloatToStr(TestArray[25]);
  210.         {Shows the value I just placed in element 25. Should be 24.5}
  211.                  TestArray.Free;
  212.         END;
  213.  
  214. PROCEDURE TForm1.Button2Click(Sender: TObject);
  215.     BEGIN
  216.         TestArray := TSingleArray.Create(Self);
  217.                  Edit1.Text := inttostr(TestArray.Size);
  218.         END;
  219.  
  220. end.
  221.  
  222. Use this however you want.
  223.  
  224. Frank Francone
  225.